﻿
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Profile;
using System.Data;
using System.Text;
using Gift.Model.ENUM;
using Gift.DAL;
using Gift.Model;


namespace WebApp.admin.report
{
    public partial class goodsReport : BaseAdmin
    {
        protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.GridView1.PageIndex = this.ddlCurrentPage.SelectedIndex;
            this.GridViewBind("");
        }


        private string GetSql()
        {
                string sWhere = string.Empty;
                if (!string.IsNullOrEmpty(this.ddlMonth.SelectedItem.Value))
                {
                    string sMonth = "";
                    if (int.Parse(this.ddlMonth.SelectedItem.Value) < 10)
                    {
                        sMonth = "0" + this.ddlMonth.SelectedItem.Value;
                    }
                    sWhere = "  substring(convert(varchar({0}),createtime,121),1,7) ='" + this.ddlYear.SelectedItem.Value + "-" + sMonth + "'";
                }
                else
                {
                    sWhere = "  substring(convert(varchar({0}),createtime,121),1,4) ='" + this.ddlYear.SelectedItem.Value + "'";
                }
                if (this.DropDownList1.SelectedItem.Value == "1")
                {
                    sWhere = string.Format(sWhere, "10");
                }
                else
                {
                    sWhere = string.Format(sWhere, "7");
                }
                string sWhereCompany = string.Empty;

                 sWhereCompany = string.Format(" and companyId = {0} ", base.Administrator.CompanyId);


                 string sql = " select  convert(varchar({2}),createtime,121) as 日期,count(1) as 订单数量 ,"
                             + " sum(orderTotal) as 金额,"
                              + " deliveryWay as 运费方式,"
                                 + " sum(deliveryMoney) as 运费 "
                             + "  from tbOrder   group by convert(varchar({2}),createtime,121),companyId,deliveryWay  "
                             + " having   {0}  {1} ";
                 if (this.DropDownList1.SelectedItem.Value == "1")
                 {
                     sql = string.Format(sql, sWhere, sWhereCompany,"10");
                 }
                 else
                 {
                     sql = string.Format(sql, sWhere, sWhereCompany,"7");
                 }

              
                return sql;
        }


        private void GridViewBind(string Sqlsort)
        {
            string sql = GetSql();
            if (Sqlsort == "")
            {
                sql = sql + " order by 日期 desc ";
            }
            else
            {
                sql = sql + Sqlsort;
            }

            DataTable table = new DataTable();
            table = SQLAccess.GreatDs(sql).Tables[0];

            // 获取GridView排序数据列及排序方向
            string sortExpression = this.GridView1.Attributes["SortExpression"];
            string sortDirection = this.GridView1.Attributes["SortDirection"];
            // 根据GridView排序数据列及排序方向设置显示的默认数据视图
            if ((!string.IsNullOrEmpty(sortExpression)) && (!string.IsNullOrEmpty(sortDirection)))
            {
                table.DefaultView.Sort = string.Format("{0} {1}", sortExpression, sortDirection);
            }

            this.GridView1.DataSource = table.DefaultView;
            this.GridView1.DataBind();
            this.LblCurrentIndex.Text = "当前第 " + ((this.GridView1.PageIndex + 1)).ToString() + "页";
            this.LblPageCount.Text = "共 " + this.GridView1.PageCount.ToString() + "页";
            this.LblRecordCount.Text = "总计 " + table.Rows.Count.ToString() + "个记录";
            if (table.Rows.Count == 0)
            {
                this.btnFirst.Visible = false;
                this.btnPrev.Visible = false;
                this.btnNext.Visible = false;
                this.btnLast.Visible = false;
                this.LblCurrentIndex.Visible = false;
                this.LblPageCount.Visible = false;
                this.LblRecordCount.Visible = false;
            }
            else if (this.GridView1.PageCount == 1)
            {
                this.btnFirst.Visible = true;
                this.btnPrev.Visible = true;
                this.btnNext.Visible = true;
                this.btnLast.Visible = true;
                this.LblCurrentIndex.Visible = true;
                this.LblPageCount.Visible = true;
                this.LblRecordCount.Visible = true;
            }
            this.btnFirst.CommandName = "1";
            this.btnPrev.CommandName = (this.GridView1.PageIndex == 0) ? "1" : this.GridView1.PageIndex.ToString();
            this.btnNext.CommandName = (this.GridView1.PageCount == 1) ? this.GridView1.PageCount.ToString() : ((this.GridView1.PageIndex + 2)).ToString();
            this.btnLast.CommandName = this.GridView1.PageCount.ToString();
            this.ddlCurrentPage.Items.Clear();
            if (this.GridView1.PageCount > 0)
            {
                for (int i = 1; i <= this.GridView1.PageCount; i++)
                {
                    this.ddlCurrentPage.Items.Add(i.ToString());
                }
                this.ddlCurrentPage.SelectedIndex = this.GridView1.PageIndex;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {

            if (!base.IsPostBack)
            {
                //  base.ValidateLoginRule(ERule.OrderStat);
                this.BindYearAndMonth();
                // this.GridViewBind("");
            }
        }

        protected void PagerButtonClick(object sender, EventArgs e)
        {
            this.GridView1.PageIndex = Convert.ToInt32(((LinkButton)sender).CommandName) - 1;
            this.GridViewBind("");
        }

        protected void ibtnSearch_Click(object sender, ImageClickEventArgs e)
        {
            this.GridViewBind("");
        }

        protected void DropDownList1_SelectedIndexChanged1(object sender, EventArgs e)
        {
            this.GridViewBind("");
        }

        protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
        {
            // 从事件参数获取排序数据列
            string sortExpression = e.SortExpression.ToString();

            // 假定为排序方向为“顺序”
            string sortDirection = "ASC";

            // “ASC”与事件参数获取到的排序方向进行比较，进行GridView排序方向参数的修改
            if (sortExpression == this.GridView1.Attributes["SortExpression"])
            {
                //获得下一次的排序状态
                sortDirection = (this.GridView1.Attributes["SortDirection"].ToString() == sortDirection ? "DESC" : "ASC");
            }

            // 重新设定GridView排序数据列及排序方向
            this.GridView1.Attributes["SortExpression"] = sortExpression;
            this.GridView1.Attributes["SortDirection"] = sortDirection;

            this.GridViewBind("");
        }

        protected void ibtnExport_Click(object sender, ImageClickEventArgs e)
        {
            string fileName = DateTime.Now.ToString("yyyyMMddhhmmss") + ".csv";//设置导出文件的名称   
            string sql = GetSql();

            sql = sql + " order by 日期 desc ";

            DataTable table = new DataTable();
            table = SQLAccess.GreatDs(sql).Tables[0];
            string sTitle = string.Empty;
            if (!string.IsNullOrEmpty(this.ddlMonth.SelectedItem.Value))
            {
                sTitle = " 订单数量统计表(" + this.ddlYear.SelectedItem.Value + "年" + this.ddlMonth.SelectedItem.Value + "月)";
            }
            else
            {
                sTitle = " 订单数量统计表(" + this.ddlYear.SelectedItem.Value + "年)";
            }
            Gift.Utile.OperateExcel.ExportCSVWithTitle(table, fileName, sTitle);
        }

        void BindYearAndMonth()
        {
            for (int i = 2011; i <= 2015; i++)
            {
                this.ddlYear.Items.Add(i.ToString());
            }

            for (int i = 1; i <= 12; i++)
            {
                this.ddlMonth.Items.Add(i.ToString());
            }
            this.ddlMonth.Items.Insert(0, new ListItem("请选取月", ""));
        }
    }
}
